# https://leetcode-cn.com/problems/palindrome-number/
class Solution:
    def reverse0(self, x: int) -> int:
        min = -2147483648
        max = 2147483647
        max_length = 10
        is_negative = False
        sx = str(x)
        if sx[0] == '-':
            sx = sx[:0:-1]
            is_negative = True
        else:
            sx = sx[::-1]

        if len(sx) > max_length:
            return 0

        if is_negative:
            result = -int(sx)
        else:
            result = int(sx)

        if min <= result <= max:
            return result
        else:
            return 0

    def reverse(self, x: int) -> int:
        """根据test文件中，性能测试的结果。这个算法又搞脑子又慢，毫无意义。"""
        y , res = abs(x), 0
        boundry = 2147483647 if x > 0 else 2147483648
        while y != 0:
            res = res*10 + y%10
            if res > boundry:
                return 0
            y //= 10
        return res if x > 0 else -res

    def reverse_two_lines(self, x: int) -> int:
        """只要两行代码"""
        y = (1, -1)[x < 0] * int(str(abs(x))[::-1])
        return y if y.bit_length() < 32 else 0

    func_name = 'reverse'
    def call_all(self, x: int) -> None:
        for name in ( i for i in dir(self) if i.startswith(self.func_name) ):
            func = getattr(self, name)
            if callable(func):
                res = func(x)
                print(name, end='(')
                print(x, end='): ')
                print(res)


if __name__ == '__main__':
    x = 12345
    Solution().call_all(x)



